package de.uni.muenster.idw2.location.wifi;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;
/**
 * Is the handler for the InfoParser and reads in the
 * information of the xml and stores each as an info
 * @author Michael Wilbers
 *
 */
public class InfoHandler extends DefaultHandler {

	
	private boolean xml = false;
	private boolean verzeichnis = false;
	private boolean titel = false;
	private boolean info = false;
	private boolean name = false;
	private boolean latitude = false;
	private boolean longitude = false;
	private boolean floor = false;
	private boolean bildX = false;
	private boolean bildY = false;
	private boolean signal1 = false;
	private boolean signal2 = false;
	private boolean signal3 = false;
	private boolean signal4 = false;
	private boolean signal5 = false;
	private boolean signal6 = false;
	private boolean ssid = false;
	private boolean value = false;
	
	
	private String targetname = null;
	private String stringlatitude = null;
	private String stringlongitude = null;
	private String stringfloor = null;
	private String stringbildX = null;
	private String stringbildY = null;
	private String stringsignal1 = null;
	private String stringsignal2 = null;
	private String stringsignal3 = null;
	private String stringsignal4 = null;
	private String stringsignal5 = null;
	private String stringsignal6 = null;
	private String signalname1 = null;
	private String signalname2 = null;
	private String signalname3 = null;
	private String signalname4 = null;
	private String signalname5 = null;
	private String signalname6 = null;
	
	private int i = 0;
	
	private WifiPositionen fingerprints = WifiPositionen.getWifiPositionen();
//	private Network network = Network.getNetwork();
	StringBuilder builder;
	StringBuilder builder2;
	StringBuilder builder3;
	StringBuilder builder4;
	StringBuilder builder5;
	

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
	
		
		
		if(localName.equals("xml")){
			this.xml = true;
		}
		else if(localName.equals("verzeichnis")){
			this.verzeichnis = true;
		}
		else if(localName.equals("titel")){
			this.titel = true;
		}
		else if(localName.equals("info")){
			this.info = true;
		}
		else if(localName.equals("name")){
			this.name = true;
		}
		else if(localName.equals("latitude")){
			this.latitude = true;
		}
		else if(localName.equals("longitude")){
			this.longitude = true;
		}
		else if(localName.equals("floor")){
			this.floor = true;
		}
		else if(localName.equals("bildX")){
			this.bildX = true;
		}
		else if(localName.equals("bildY")){
			this.bildY = true;
		}
		else if(localName.equals("signal1")){
			this.signal1 = true;
		}
		else if(localName.equals("signal2")){
			this.signal2 = true;
		}
		else if(localName.equals("signal3")){
			this.signal3 = true;
		}
		else if(localName.equals("signal4")){
			this.signal4 = true;
		}
		else if(localName.equals("signal5")){
			this.signal5 = true;
		}
		else if(localName.equals("signal6")){
			this.signal6 = true;
		}
		else if(localName.equals("SSID")){
			this.ssid = true;
		}
		else if(localName.equals("value")){
			this.value = true;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		if(localName.equals("xml")){
			this.xml = false;
		}
		else if(localName.equals("verzeichnis")){
			this.verzeichnis = false;
		}
		else if(localName.equals("titel")){
			this.titel = false;
		}
		else if(localName.equals("info")){
			this.info = false;
			// when the complete info is parsed, the processing is started
			this.processingInfo();
		}
		else if(localName.equals("name")){
			this.name = false;
		}
		else if(localName.equals("latitude")){
			this.latitude = false;
		}
		else if(localName.equals("longitude")){
			this.longitude = false;
		}
		else if(localName.equals("floor")){
			this.floor = false;
		}
		else if(localName.equals("bildX")){
			this.bildX = false;
			stringbildX = builder.toString();
			builder.setLength(0);
		}
		else if(localName.equals("bildY")){
			this.bildY = false;
		}
		else if(localName.equals("signal1")){
			this.signal1 = false;
			stringsignal1 = builder2.toString();
			builder2.setLength(0);
		}
		else if(localName.equals("signal2")){
			this.signal2 = false;
		}
		else if(localName.equals("signal3")){
			this.signal3 = false;
			stringsignal3 = builder3.toString();
			builder3.setLength(0);
		}
		else if(localName.equals("signal4")){
			this.signal4 = false;
		}
		else if(localName.equals("signal5")){
			this.signal5 = false;
			stringsignal5 = builder4.toString();
			builder4.setLength(0);
		}
		else if(localName.equals("signal6")){
			this.signal6 = false;
		}
		else if(localName.equals("SSID")){
			this.ssid = false;
		}
		else if(localName.equals("value")){
			this.value = false;
		}


		
	}
	
	@Override
    public void startDocument() throws SAXException {
        super.startDocument();
        // prepare StringBuilder for the longer text parts
        
        builder = new StringBuilder();
        builder2 = new StringBuilder();
        builder3 = new StringBuilder();
        builder4 = new StringBuilder();
        builder5 = new StringBuilder();
    }

	

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		//Log.v("show", "in the parser");
		if(this.bildX){
			builder.append(ch, start, length);
		}
		else if(this.name){
			this.targetname = new String(ch, start, length);
		}
		else if(this.latitude){
			this.stringlatitude = new String(ch, start, length);
		}
		else if(this.longitude){
			this.stringlongitude = new String(ch, start, length);
		}
		else if(this.floor){
			this.stringfloor = new String(ch, start, length);
		}
		else if(this.bildY){
			this.stringbildY = new String(ch, start, length);
		}
		else if(this.signal1){
			if(this.ssid){
				signalname1 = new String(ch, start, length);
			}
			if(this.value){
				builder2.append(ch, start, length);
			}
		}
		else if(this.signal2){
			if(this.ssid){
				signalname2 = new String(ch, start, length);
			}
			if(this.value){
				this.stringsignal2 = new String(ch, start, length);
			}	
		}
		else if(this.signal3){
			if(this.ssid){
				signalname3 = new String(ch, start, length);
			}
			if(this.value){
				builder3.append(ch, start, length);
			}	
		}
		else if(this.signal4){
			if(this.ssid){
				signalname4 = new String(ch, start, length);
			}
			if(this.value){
				this.stringsignal4 = new String(ch, start, length);
			}	
		}
		else if(this.signal5){
			if(this.ssid){
				signalname5 = new String(ch, start, length);
			}
			if(this.value){
				builder4.append(ch, start, length);
			}	
		}
		else if(this.signal6){
			if(this.ssid){
				signalname6 = new String(ch, start, length);
			}
			if(this.value){
				this.stringsignal6 = new String(ch, start, length);
			}	
		}
		
	}
	/**
	 * The method processingInfo builds an Info out of the parsed information
	 */
	public void processingInfo(){
		
			WGSPosition w = new WGSPosition(Double.parseDouble(stringlatitude), Double.parseDouble(stringlongitude), Integer.parseInt(stringfloor));
			BildPosition b = new BildPosition(Integer.parseInt(stringbildX),Integer.parseInt(stringbildY),Integer.parseInt(stringfloor));
			int s1 = Integer.parseInt(stringsignal1);
			Signal si1 = new Signal(signalname1, s1);
			int s2 = Integer.parseInt(stringsignal2);
			Signal si2 = new Signal(signalname2, s2);
			int s3 = Integer.parseInt(stringsignal3);
			Signal si3 = new Signal(signalname3, s3);
			int s4 = Integer.parseInt(stringsignal4);
			Signal si4 = new Signal(signalname4, s4);
			int s5 = Integer.parseInt(stringsignal5);
			Signal si5 = new Signal(signalname5, s5);
			int s6 = Integer.parseInt(stringsignal6);
			Signal si6 = new Signal(signalname6, s6);
        	Fingerprint p = new Fingerprint(targetname, w, b, si1, si2, si3, si4, si5, si6);
        	
        	//Log.v("show", targetname);
        	//Log.v("show", stringlatitude);
        	//Log.v("show", stringlongitude);
        	//Log.v("show", stringfloor);
        	//Log.v("show", stringbildX);
        	//Log.v("show", stringbildY);
        	//Log.v("show", signalname1);
        	//Log.v("show", stringsignal1);
        	//Log.v("show", signalname2);
        	//Log.v("show", stringsignal2);
        	//Log.v("show", signalname3);
        	//Log.v("show", stringsignal3);
        	//Log.v("show", signalname4);
        	//Log.v("show", stringsignal4);
        	//Log.v("show", signalname5);
        	//Log.v("show", stringsignal5);
        	//Log.v("show", signalname6);
        	//Log.v("show", stringsignal6);
        	
        	targetname = null;
        	stringlatitude = null;
        	stringlongitude = null;
        	stringfloor = null;
        	stringbildX = null;
        	stringbildY = null;
        	stringsignal1 = null;
        	stringsignal2 = null;
        	stringsignal3 = null;
        	stringsignal4 = null;
        	stringsignal5 = null;
        	stringsignal6 = null;
        	signalname1 = null;
        	signalname2 = null;
        	signalname3 = null;
        	signalname4 = null;
        	signalname5 = null;
        	signalname6 = null;
        	
        	//i++;
        	//Log.v("show", "am ende des parsers");
        	//Log.v("show", Integer.toString(i));
        	fingerprints.addFingerprint(p);
        
	}
	
	
}
